package handler

import (
	"apibe/single/src/v1/factory"
	"apibe/single/src/v1/factory/roads"
	"apibe/single/src/v1/factory/shortcut"
	"apibe/single/src/v1/factory/tools"
	"apibe/single/src/v1/plugin/postman"
	"apibe/single/src/v1/plugin/task"
	"apibe/single/src/v1/plugin/tourist"
	"apibe/single/src/v1/service"
	"apibe/single/src/v1/share/crypt"
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

// ApiSelectorHandler
// 根据类型获取input框提示词
func ApiSelectorHandler(action, selector string) interface{} {
	switch action {
	case "query":
		switch selector {
		case "tool":
			return tools.GetTools()
		case "request":
			return roads.GetLeaves()
		case "response":
			return roads.GetReturns()
		case "url":
			return tourist.GetUrls()
		default:
			return nil
		}
	case "add":
		switch selector {
		default:
			return nil
		}
	case "update":
		switch selector {
		default:
			return nil
		}
	case "remove":
		switch selector {
		default:
			return nil
		}
	default:
		return nil
	}
}

const (
	_task        = "task"
	_design      = "design"
	_needCheck   = "NeedCheck"
	_working     = "HaveChecked"
	_unUsed      = "Unused"
	_haveDeleted = "HaveDeleted"
)

//	ApiStatusHandler
//	修改表状态
func ApiStatusHandler(work string, action string, id string, claims *crypt.CustomClaims) error {
	design, err := service.NewService(service.DesignService)
	if err != nil {
		return err
	}
	objID, err := primitive.ObjectIDFromHex(id)
	if err != nil {
		return err
	}
	switch action {
	case _needCheck:
		switch work {
		case _task:
			err = task.UpdateStatus(id, factory.NeedCheck)
			break
		case _design:
			_, err = design.UpdateStatus(objID, claims, factory.NeedCheck)
			break
		}
	case _working:
		switch work {
		case _task:
			err = task.UpdateStatus(id, factory.Working)
			break
		case _design:
			_, err = design.UpdateStatus(objID, claims, factory.Working)
			break
		}
	case _unUsed:
		switch work {
		case _task:
			err = task.UpdateStatus(id, factory.Unused)
			break
		case _design:
			_, err = design.UpdateStatus(objID, claims, factory.Unused)
			break
		}
	case _haveDeleted:
		switch work {
		case _task:
			err = task.UpdateStatus(id, factory.Deleted)
			break
		case _design:
			_, err = design.UpdateStatus(objID, claims, factory.Deleted)
			break
		}
	}
	if err != nil {
		return err
	}
	return nil
}

// ApiCacheResponseHandler
// 清除返回值缓存
func ApiCacheResponseHandler(action string, id string) int64 {
	c := shortcut.Cache{}
	switch action {
	case "remove_one":
		return c.CacheRemoveOne(id)
	case "remove_many":
		return c.CacheRemoveMany(id)
	case "remove_all":
		return c.CacheRemoveAll()
	default:
		return 0
	}
}

// ApiPostDownloadHandler
// 导出postman文件
func ApiPostDownloadHandler(name, url, token string, apiName []string) (*postman.Postman, error) {
	designService, err := service.NewService(service.DesignService)
	if err != nil {
		return nil, err
	}
	filter := bson.M{"api_name": bson.M{"$in": apiName}}
	findManyFilter, _, err := designService.FindManyFilter(filter, 1, 100)
	if err != nil {
		return nil, err
	}
	var ds []factory.Design
	err = findManyFilter.All(context.Background(), &ds)
	if err != nil {
		return nil, err
	}
	pm := postman.Postman{}
	pm.New(name)
	pm.GetPostman(url, token, &ds)
	return &pm, nil
}

// ApiGetWatchHandler
// 获取当前接口的监控信息
func ApiGetWatchHandler(apiName string) (map[string]interface{}, error) {
	return shortcut.GetWatch(apiName)
}
